package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.MenuButtonMapper;
import com.zhijian.medical.entity.po.MenuButton;
import com.zhijian.medical.entity.vo.request.MenuButtonSearchVo;
import com.zhijian.medical.entity.vo.response.MenuButtonVo;
import com.zhijian.medical.service.MenuButtonService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 菜单-按钮关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-23 15:38:41
 */
@Slf4j
@Service
public class MenuButtonServiceImpl implements MenuButtonService {

    @Autowired
    private MenuButtonMapper mapper;

    @Override
    public MenuButton selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public MenuButtonVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(MenuButton menuButton) {
        Date now = new Date();
        menuButton.setCreateTime(now);
        menuButton.setUpdateTime(now);
        return mapper.insert(menuButton) > 0 ? menuButton.getId() : 0L;
    }

    @Override
    public boolean update(MenuButton menuButton) {
        Date now = new Date();
        menuButton.setUpdateTime(now);
        return mapper.updateByPrimaryKey(menuButton) != 0;
    }

    @Override
    public Integer count(MenuButtonSearchVo menuButtonSearchVo) {
        return mapper.count(menuButtonSearchVo);
    }

    @Override
    public List<MenuButtonVo> selectList(MenuButtonSearchVo menuButtonSearchVo) {
        return mapper.selectList(menuButtonSearchVo);
    }

    @Override
    public Page<MenuButtonVo> selectPage(MenuButtonSearchVo menuButtonSearchVo) {
        //先直接查总数
        int count = count(menuButtonSearchVo);
        //分页
        Page<MenuButtonVo> page = new Page<>(true, count, menuButtonSearchVo.getPageNum(), menuButtonSearchVo.getPageSize());
        menuButtonSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MenuButtonVo> responseVos = this.selectList(menuButtonSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean deleteByMenuId(Long menuId) {
        return mapper.deleteByMenuId(menuId);
    }

}